const express = require('express')
const morgan = require('morgan')
const cors = require('cors')

require('./models')
const app = express()
const logger = require('./logger')
const productRoutes = require('./routes/productRoutes')
const orderRoutes = require('./routes/orderRoutes')
const paymentRoutes = require('./routes/paymentRoutes')


app.use(morgan('dev'))
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.get('/', async (req, res) => {
  res.send('wechatpay')
})

require('./swagger')(app)
app.use('/api/products', productRoutes)
app.use('/api/orders', orderRoutes)
app.use('/api/payment', paymentRoutes)
app.use((error, req, res, next) => {
  logger.error('Error:', error)
  res.status(500).send("Internal Server Error")
})

app.use((req, res) => {
  res.status(404).send("404:Page Not Found")
})


module.exports = app